package com.example.web.office_file.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.ResultUtils;
import com.example.utils.ResultVo;
import com.example.web.office_file.entity.FileParam;
import com.example.web.office_file.entity.OfficeFile;
import com.example.web.office_file.service.IOfficeFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LS
 * @since 2024-03-05
 */
@Api(tags = "文件管理接口")
@RestController
@RequestMapping("/api/office-file")
public class OfficeFileController {

    @Resource
    private IOfficeFileService officeFileService;

    /**
     * 分页查询用户文件（文件夹优先）
     */
    @ApiOperation("获取文件列表")
    @GetMapping("/getList")
    public ResultVo getFileByPage(FileParam fileParam) {
        Integer currentPage = fileParam.getCurrentPage();
        Integer pageSize = fileParam.getPageSize();
        String curDir = fileParam.getCurDir();
        Integer op = fileParam.getOp();
        Page<OfficeFile> filePage = officeFileService.getFileByPage(currentPage, pageSize, curDir, op);
        return ResultUtils.success("查询成功", filePage);
    }

    /**
     * 创建目录
     */
    @ApiOperation("新建文件夹")
    @PostMapping("/createDir")
    public ResultVo createDir(@RequestBody Map<String, String> map) {
        String msg = officeFileService.createDir(map);
        if (msg.equals("文件夹创建成功！")) {
            return ResultUtils.success("文件夹创建成功!");
        }
        return ResultUtils.error(msg);
    }

    /**
     * 文件上传
     *
     * @param uploadFile 文件
     * @param curDir     当前目录
     */
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public ResultVo upload(MultipartFile uploadFile, @RequestParam String curDir, @RequestParam String creator) {
        if (officeFileService.upload(uploadFile, curDir, creator))
            return ResultUtils.success("上传成功!");
        else
            return ResultUtils.error("上传失败!");
    }

    /**
     * 文件下载
     */
    @ApiOperation("文件下载")
    @GetMapping("/download/{id}")
    public void download(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
        officeFileService.download(id, request, response);
    }

    /**
     * 根据指定字段查询
     */
    @ApiOperation("根据指定字段查询")
    @GetMapping("/getConditionData")
    public ResultVo getConditionData(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String fileName) {
        Page<OfficeFile> page = officeFileService.findByFileNameLike(pageNum, pageSize, fileName);
        return ResultUtils.success("查询成功！", page);
    }

    /**
     * 删除文件或目录
     */
    @ApiOperation("删除文件或目录")
    @DeleteMapping("/{id}")
    public ResultVo deleteFile(@PathVariable Integer id) {
        if (officeFileService.removeById(id)) {
            return ResultUtils.success("删除成功！");
        }
        return ResultUtils.error("删除失败！");
    }

    /**
     * 通过ids删除文件或目录
     */
    @ApiOperation("批量删除文件")
    @DeleteMapping("/deleteList")
    public ResultVo deleteList(@RequestBody List<Integer> ids) {
        if (officeFileService.removeByIds(ids))
            return ResultUtils.success("删除成功！");
        return ResultUtils.error("删除失败！");
    }


}

